package com.itgacl.magic4j.libcommon.component.ws;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * websocket配置
 */
@Configuration
@ConditionalOnProperty(prefix = "ws", value = "enable", havingValue = "true")
@EnableWebSocket
public class WsConfig implements WebSocketConfigurer {

    @Autowired
    private WsHandler wsHandler;

    @Autowired
    private WsInterceptor wsInterceptor;

    @Value("${ws.uri}")
    private String wsUri;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(wsHandler, wsUri)//添加ws的handler处理类，第二个参数是暴露出的ws访问路径
                .addInterceptors(wsInterceptor)//配置拦截器
                .setAllowedOrigins("*");//允许跨域
    }
}